﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
using System.IO;

namespace Workflow.Project
{
    public partial class SampleOut : System.Web.UI.Page
    {
        public PJSampleOut result = null;
        private PJSampleOutManager objPJSampleOutManager = new PJSampleOutManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        public int workParentId;
        public int fromPage;
        public int childListCount;
        public int pjCount;
        public int statusNo;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            //txtWorkParentId.Text = Convert.ToString(workParentId);
            fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
            if(!IsPostBack)
            {
                try
                {
                    pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 9, 16) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 16) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 7, 16) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 8, 16);
                    statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    result = objPJSampleOutManager.GetSampleOut(workParentId);
                    List<UserInfo> personList = objUserTeamRoleManager.GetUserInfo(9, 21, 16);
                    ddlCustomerService.DataSource = personList;
                    ddlCustomerService.DataTextField = "DisplayName";
                    ddlCustomerService.DataValueField = "UserName";
                    ddlCustomerService.DataBind();
                    if (result != null)
                    {
                        txtSerialNum.Text = result.SerialNum;
                        ddlOrderType.SelectedItem.Text = result.OrderType;
                        txtData.Text = result.Statues;
                        txtOrderStartSerialNum.Text = result.OrderStartSerialNum;
                        txtCustomer.Text = result.Customer;
                        txtCustomerAddress.Text = result.CustomerAddress;
                        txtPerson.Text = result.Person;
                        txtSaleOrder.Text = result.SaleOrder;
                        txtExpressNo.Text = result.ExpressNo;
                        txtTel.Text = result.Tel;


                        if (!string.IsNullOrEmpty(result.CustomerService)) { ddlCustomerService.SelectedItem.Text = result.CustomerService;}

                        if (!string.IsNullOrEmpty(result.CustomerService)) { ddlCustomerService.Text = result.CustomerService; }
                        else
                        {
                            personList.Insert(0, new UserInfo() { UserName = "", DisplayName = "" });
                            ddlCustomerService.SelectedIndex = 0;
                            ddlCustomerService.DataBind();
                        }
                        
                        txtBill.Text = result.Bill;
                        txtBill2.Text = result.Bill2;
                        txt0101.Text = result.ChildList[0].CustomerOrder;
                        txt0102.Text = result.ChildList[0].Maktx;
                        txt0103.Text = result.ChildList[0].Description;
                        txt0104.Text = Convert.ToString(result.ChildList[0].Num);
                        txt0105.Text = result.ChildList[0].Rev;
                        txt0106.Text = result.ChildList[0].Ps;
                        childListCount = result.ChildList.Count;
                        if (result.ChildList.Count >= 2)
                        {
                            txt0201.Text = result.ChildList[1].CustomerOrder;
                            txt0202.Text = result.ChildList[1].Maktx;
                            txt0203.Text = result.ChildList[1].Description;
                            txt0204.Text = Convert.ToString(result.ChildList[1].Num);
                            txt0205.Text = result.ChildList[1].Rev;
                            txt0206.Text = result.ChildList[1].Ps;
                        }
                        if (result.ChildList.Count >= 3)
                        {
                            txt0301.Text = result.ChildList[2].CustomerOrder;
                            txt0302.Text = result.ChildList[2].Maktx;
                            txt0303.Text = result.ChildList[2].Description;
                            txt0304.Text = Convert.ToString(result.ChildList[2].Num);
                            txt0305.Text = result.ChildList[2].Rev;
                            txt0306.Text = result.ChildList[2].Ps;
                        }
                        if (result.ChildList.Count >= 4)
                        {
                            txt0401.Text = result.ChildList[3].CustomerOrder;
                            txt0402.Text = result.ChildList[3].Maktx;
                            txt0403.Text = result.ChildList[3].Description;
                            txt0404.Text = Convert.ToString(result.ChildList[3].Num);
                            txt0405.Text = result.ChildList[3].Rev;
                            txt0406.Text = result.ChildList[3].Ps;
                        }
                        if (result.ChildList.Count >= 5)
                        {
                            txt0501.Text = result.ChildList[4].CustomerOrder;
                            txt0502.Text = result.ChildList[4].Maktx;
                            txt0503.Text = result.ChildList[4].Description;
                            txt0504.Text = Convert.ToString(result.ChildList[4].Num);
                            txt0505.Text = result.ChildList[4].Rev;
                            txt0506.Text = result.ChildList[4].Ps;
                        }
                        if (result.ChildList.Count >= 6)
                        {
                            txt0601.Text = result.ChildList[5].CustomerOrder;
                            txt0602.Text = result.ChildList[5].Maktx;
                            txt0603.Text = result.ChildList[5].Description;
                            txt0604.Text = Convert.ToString(result.ChildList[5].Num);
                            txt0605.Text = result.ChildList[5].Rev;
                            txt0606.Text = result.ChildList[5].Ps;
                        }
                        if (result.ChildList.Count >= 7)
                        {
                            txt0701.Text = result.ChildList[6].CustomerOrder;
                            txt0702.Text = result.ChildList[6].Maktx;
                            txt0703.Text = result.ChildList[6].Description;
                            txt0704.Text = Convert.ToString(result.ChildList[6].Num);
                            txt0705.Text = result.ChildList[6].Rev;
                            txt0706.Text = result.ChildList[6].Ps;
                        }
                        if (result.ChildList.Count >= 8)
                        {
                            txt0801.Text = result.ChildList[7].CustomerOrder;
                            txt0802.Text = result.ChildList[7].Maktx;
                            txt0803.Text = result.ChildList[7].Description;
                            txt0804.Text = Convert.ToString(result.ChildList[7].Num);
                            txt0805.Text = result.ChildList[7].Rev;
                            txt0806.Text = result.ChildList[7].Ps;
                        }
                        if (result.ChildList.Count >= 9)
                        {
                            txt0901.Text = result.ChildList[8].CustomerOrder;
                            txt0902.Text = result.ChildList[8].Maktx;
                            txt0903.Text = result.ChildList[8].Description;
                            txt0904.Text = Convert.ToString(result.ChildList[8].Num);
                            txt0905.Text = result.ChildList[8].Rev;
                            txt0906.Text = result.ChildList[8].Ps;
                        }
                        if (result.ChildList.Count >= 10)
                        {
                            txt1001.Text = result.ChildList[9].CustomerOrder;
                            txt1002.Text = result.ChildList[9].Maktx;
                            txt1003.Text = result.ChildList[9].Description;
                            txt1004.Text = Convert.ToString(result.ChildList[9].Num);
                            txt1005.Text = result.ChildList[9].Rev;
                            txt1006.Text = result.ChildList[9].Ps;
                        }
                        if (fromPage == 0 || statusNo != 100)
                        {
                            if (pjCount > 0 && statusNo < 1)
                            {
                                EnabledControl2();
                                Update1.Visible = true;
                            }
                            if (pjCount > 0 && statusNo == 2) //客服
                            {
                                txtSaleOrder.Enabled = true;
                                Update2.Visible = true;
                            }
                            if (pjCount > 0 && statusNo == 4) //项目仓库
                            {
                                txtBill2.Enabled = true;
                                Update3.Visible = true;
                                
                            }
                            if (pjCount > 0 && statusNo == 5) //成品仓库
                            {
                                txtBill.Enabled = true;
                                BtnSubmit2.Visible = true;
                                Export1.Visible = true;
                                Export2.Visible = true;
                            }

                        }
                        if (statusNo == 100) //已完成
                        {
                            Update3.Visible = true;

                        }
                    }
                    else
                    {
                        if (statusNo == 0) 

                        {
                            personList.Insert(0, new UserInfo() { UserName = "", DisplayName = "" });
                            ddlCustomerService.DataBind();
                            EnabledControl1();
                            EnabledControl2();
                            Create.Visible = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
             

        }
        protected void BtnSubmit1_Click(object sender, EventArgs e)
        {
            PJSampleOut objPJSampleOutCreate = new PJSampleOut()
            {
                WorkParentId = workParentId,
                SerialNum = ddlOrderType.SelectedValue + txtOrderStartSerialNum.Text.Trim().Substring(txtOrderStartSerialNum.Text.Length - 2, 2),
                OrderType = ddlOrderType.SelectedItem.Text,
                OrderStartSerialNum = txtOrderStartSerialNum.Text.Trim().ToString(),
                OutDate = Request.Params.Get("txtOutDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtOutDate")),
                Customer = txtCustomer.Text.Trim().ToString(),
                CustomerAddress = txtCustomerAddress.Text.Trim().ToString(),
                Person = txtPerson.Text.Trim().ToString(),
                //SaleOrder = txtSaleOrder.Text.Trim().ToString(),
                //Bill = txtBill.Text.Trim().ToString(),
                CustomerService = ddlCustomerService.SelectedItem.Text,
                ExpressNo = txtExpressNo.Text.Trim(),
                Tel = txtTel.Text.Trim(),
                ChildList = new List<PJSampleChildOut>()
            };
            GetInfo(ref objPJSampleOutCreate);
            try
            {
                string result = objPJSampleOutManager.InsertIsNoSucess(objPJSampleOutCreate);
                if(result!="false")
                {
                    string msg = string.Format("<script>alert('流水号:{0}保存成功！')</script>", result);
                    string str = string.Format("<script>window.close();</script>");
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                }
                else
                {
                    string msg = string.Format("<script>alert('保存失败！')</script>", result);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        void EnabledControl1()
        {
            ddlOrderType.Enabled = true;
            txtOrderStartSerialNum.Enabled = true;
            
        }
        void EnabledControl2()
        {
            txtCustomer.Enabled = true;
            txtCustomerAddress.Enabled = true;
            txtPerson.Enabled = true;
            ddlCustomerService.Enabled = true;
            txtExpressNo.Enabled = true;
            txtTel.Enabled = true;
            txt0101.Enabled = true;
            txt0102.Enabled = true;
            txt0103.Enabled = true;
            txt0104.Enabled = true;
            txt0105.Enabled = true;
            txt0106.Enabled = true;
            txt0201.Enabled = true;
            txt0202.Enabled = true;
            txt0203.Enabled = true;
            txt0204.Enabled = true;
            txt0205.Enabled = true;
            txt0206.Enabled = true;
            txt0301.Enabled = true;
            txt0302.Enabled = true;
            txt0303.Enabled = true;
            txt0304.Enabled = true;
            txt0305.Enabled = true;
            txt0306.Enabled = true;
            txt0401.Enabled = true;
            txt0402.Enabled = true;
            txt0403.Enabled = true;
            txt0404.Enabled = true;
            txt0405.Enabled = true;
            txt0406.Enabled = true;
            txt0501.Enabled = true;
            txt0502.Enabled = true;
            txt0503.Enabled = true;
            txt0504.Enabled = true;
            txt0505.Enabled = true;
            txt0506.Enabled = true;
            txt0601.Enabled = true;
            txt0602.Enabled = true;
            txt0603.Enabled = true;
            txt0604.Enabled = true;
            txt0605.Enabled = true;
            txt0606.Enabled = true;
            txt0701.Enabled = true;
            txt0702.Enabled = true;
            txt0703.Enabled = true;
            txt0704.Enabled = true;
            txt0705.Enabled = true;
            txt0706.Enabled = true;
            txt0801.Enabled = true;
            txt0802.Enabled = true;
            txt0803.Enabled = true;
            txt0804.Enabled = true;
            txt0805.Enabled = true;
            txt0806.Enabled = true;
            txt0901.Enabled = true;
            txt0902.Enabled = true;
            txt0903.Enabled = true;
            txt0904.Enabled = true;
            txt0905.Enabled = true;
            txt0906.Enabled = true;
            txt1001.Enabled = true;
            txt1002.Enabled = true;
            txt1003.Enabled = true;
            txt1004.Enabled = true;
            txt1005.Enabled = true;
            txt1006.Enabled = true;
            
        }
        protected void Update1_Click(object sender, EventArgs e)
        {
            PJSampleOut objPJSampleOutUpdate = new PJSampleOut()
            {
                WorkParentId = workParentId,
                SerialNum = txtSerialNum.Text.ToString().Trim(),
                OutDate = Request.Params.Get("txtOutDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtOutDate")),
                Customer = txtCustomer.Text.Trim().ToString(),
                CustomerAddress = txtCustomerAddress.Text.Trim().ToString(),
                Person = txtPerson.Text.Trim().ToString(),
                //SaleOrder = txtSaleOrder.Text.Trim().ToString(),
                //Bill = txtBill.Text.Trim().ToString(),
                CustomerService = ddlCustomerService.SelectedItem.Text,
                ExpressNo = txtExpressNo.Text.Trim().ToString(),
                Tel = txtTel.Text.Trim().ToString(),
                ChildList = new List<PJSampleChildOut>()
            };
            GetInfo(ref objPJSampleOutUpdate);
            try
            {
                objPJSampleOutManager.UpdateInfo(objPJSampleOutUpdate);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        protected void Update2_Click(object sender, EventArgs e)
        {
            string saleOrder = txtSaleOrder.Text.Trim().ToString();
            try
            {
                objPJSampleOutManager.UpdateSaleOrder(saleOrder, workParentId);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        protected void BtnSubmit2_Click(object sender, EventArgs e)
        {
            try
            {
                string bill = txtBill.Text.Trim().ToString();
                objPJSampleOutManager.UpdateBill(bill, workParentId);
                objPJSampleOutManager.UpdateStatues(workParentId);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        protected void Update3_Click(object sender, EventArgs e)
        {
            try
            {
                string bill2 = txtBill2.Text.Trim().ToString();
                objPJSampleOutManager.UpdateBill2(bill2, Convert.ToInt32(workParentId));
                
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        void GetInfo(ref PJSampleOut objPJSampleOutCreate)
        {
            
            if (txt0101.Text.Trim().ToString().Length > 0)
            {

                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 1,
                    CustomerOrder = txt0101.Text.Trim().ToString(),
                    Maktx = txt0102.Text.Trim().ToString(),
                    Description = txt0103.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0104.Text.Trim().ToString())? 0 : Convert.ToInt32(txt0104.Text.Trim().ToString()),
                    Rev = txt0105.Text.Trim().ToUpper().ToString(),
                    Ps = txt0106.Text.Trim().ToString()
                });
            }
            if (txt0201.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 2,
                    CustomerOrder = txt0201.Text.Trim().ToString(),
                    Maktx = txt0202.Text.Trim().ToString(),
                    Description = txt0203.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0204.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0204.Text.Trim().ToString()),
                    Rev = txt0205.Text.Trim().ToUpper().ToString(),
                    Ps = txt0206.Text.Trim().ToString()
                });
            }
            if (txt0301.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 3,
                    CustomerOrder = txt0301.Text.Trim().ToString(),
                    Maktx = txt0302.Text.Trim().ToString(),
                    Description = txt0303.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0304.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0304.Text.Trim().ToString()),
                    Rev = txt0305.Text.Trim().ToUpper().ToString(),
                    Ps = txt0306.Text.Trim().ToString()
                });
            }
            if (txt0401.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 4,
                    CustomerOrder = txt0401.Text.Trim().ToString(),
                    Maktx = txt0402.Text.Trim().ToString(),
                    Description = txt0403.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0404.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0404.Text.Trim().ToString()),
                    Rev = txt0405.Text.Trim().ToUpper().ToString(),
                    Ps = txt0406.Text.Trim().ToString()
                });
            }
            if (txt0501.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 5,
                    CustomerOrder = txt0501.Text.Trim().ToString(),
                    Maktx = txt0502.Text.Trim().ToString(),
                    Description = txt0503.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0504.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0504.Text.Trim().ToString()),
                    Rev = txt0505.Text.Trim().ToUpper().ToString(),
                    Ps = txt0506.Text.Trim().ToString()
                });
            }
            if (txt0601.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 6,
                    CustomerOrder = txt0601.Text.Trim().ToString(),
                    Maktx = txt0602.Text.Trim().ToString(),
                    Description = txt0603.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0604.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0604.Text.Trim().ToString()),
                    Rev = txt0605.Text.Trim().ToUpper().ToString(),
                    Ps = txt0606.Text.Trim().ToString()
                });
            }
            if (txt0701.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 7,
                    CustomerOrder = txt0701.Text.Trim().ToString(),
                    Maktx = txt0702.Text.Trim().ToString(),
                    Description = txt0703.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0704.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0704.Text.Trim().ToString()),
                    Rev = txt0705.Text.Trim().ToUpper().ToString(),
                    Ps = txt0706.Text.Trim().ToString()
                });
            }
            if (txt0801.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 8,
                    CustomerOrder = txt0801.Text.Trim().ToString(),
                    Maktx = txt0802.Text.Trim().ToString(),
                    Description = txt0803.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0804.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0804.Text.Trim().ToString()),
                    Rev = txt0805.Text.Trim().ToUpper().ToString(),
                    Ps = txt0806.Text.Trim().ToString()
                });
            }
            if (txt0901.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 9,
                    CustomerOrder = txt0901.Text.Trim().ToString(),
                    Maktx = txt0902.Text.Trim().ToString(),
                    Description = txt0903.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt0904.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt0904.Text.Trim().ToString()),
                    Rev = txt0905.Text.Trim().ToUpper().ToString(),
                    Ps = txt0906.Text.Trim().ToString()
                });
            }
            if (txt1001.Text.Trim().ToString().Length > 0)
            {
                objPJSampleOutCreate.ChildList.Add(new PJSampleChildOut()
                {
                    Row = 10,
                    CustomerOrder = txt1001.Text.Trim().ToString(),
                    Maktx = txt1002.Text.Trim().ToString(),
                    Description = txt1003.Text.Trim().ToString(),
                    Num = string.IsNullOrEmpty(txt1004.Text.Trim().ToString()) ? 0 : Convert.ToInt32(txt1004.Text.Trim().ToString()),
                    Rev = txt1005.Text.Trim().ToUpper().ToString(),
                    Ps = txt1006.Text.Trim().ToString()
                });
            }
        }

        protected void Export1_Click(object sender, EventArgs e)
        {
            List<PJSampleExport1> exportResult = new List<PJSampleExport1>();
            try
            {
                exportResult = objPJSampleOutManager.ExportSampleOut1(workParentId);
                GridView gridView1 = new GridView();
                gridView1.DataSource = exportResult;
                gridView1.DataBind();
                ExportToExcel(gridView1);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
            
        }
        protected void Export2_Click(object sender, EventArgs e)
        {
            List<PJSampleExport2> exportResult = new List<PJSampleExport2>();
            try
            {
                exportResult = objPJSampleOutManager.ExportSampleOut2(workParentId);
                GridView gridView1 = new GridView();
                gridView1.DataSource = exportResult;
                gridView1.DataBind();
                ExportToExcel(gridView1);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        public void ExportToExcel(GridView GridView)
        {
            string excelname = DateTime.Now.ToString("yyyyMMddhhmm");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=  " + excelname + " .xls");
            Response.Charset = "gb2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/type.xls";//设置输出文件类型为excel文件      
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }

        

        
       
    }
}